package com.mioto.ld.module.personal.controller;

import com.mioto.ld.exception.BasicException;
import com.mioto.ld.module.personal.service.IFunctionService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author mioto-qinxj
 * @date 2024/5/14
 * @desc
 */
@RestController
@RequestMapping("/role/function")
@Api(tags = "角色权限管理")
public class RoleFunctionController {
    @Resource
    private IFunctionService functionService;

    @GetMapping("/{roleId}")
    @ApiOperation(value="根据角色查询权限列表")
    public ResultData findFunctionsByRole(@PathVariable("roleId")Integer roleId){
        return ResultData.success(functionService.findFunctionsByRole(roleId));
    }

    @PostMapping
    @ApiOperation(value="修改角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name="roleId",value = "角色id",dataType="Integer", paramType = "query",required=true),
            @ApiImplicitParam(name="functionIds",value = "已选中的菜单id",dataType="Integer",allowMultiple = true, paramType = "query")
    })
    public ResultData updatePermission (Integer roleId,Integer... functionIds){
        return Optional.of(functionService.updatePermission(roleId,functionIds)).filter(count -> count > 0)
                .map(count -> ResultData.success()).orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }
}
